//
//  MQReadMainViewController.m
//  MQ
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 徐永祥. All rights reserved.
//

#import "MQReadMainViewController.h"
#import "MQHomeViewController.h"
#import "MQClassicsAnaViewController.h"
#import "MQEmotionViewController.h"
#import "LewPopupViewAnimationDrop.h"
#import "MQPopupView.h"
@interface MQReadMainViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) UISegmentedControl *segment;
@property (nonatomic, strong) MQHomeViewController*homeVC;
@property (nonatomic, strong) MQClassicsAnaViewController *classicsAnaVC;
@property (nonatomic, strong) MQEmotionViewController *emtionVC;
//@property (nonatomic, strong) MQKnowledgeViewController *knowledgeVC;
@end

@implementation MQReadMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"daohanglan"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(setting:)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
    NSArray *array = @[@"经典语录", @"段子", @"情感专栏"];
    _segment = [[UISegmentedControl alloc] initWithItems:array];
    _segment.frame = CGRectMake(SCREEN_WIDTH/2-100, 5, 200, 30);
    _segment.selectedSegmentIndex = 1;
    [_segment addTarget:self action:@selector(changeViews:) forControlEvents:(UIControlEventValueChanged)];
    _segment.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar addSubview:_segment];
    
    self.mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 3, 0);
    _mainScrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.delegate = self;
    [self addSubViews];
    [self.view addSubview:_mainScrollView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
// 添加四个子视图
- (void)addSubViews{
    _homeVC = [[MQHomeViewController alloc] initWithStyle:(UITableViewStylePlain)];
    _homeVC.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _homeVC.readMainVC = self;
    [_mainScrollView addSubview:_homeVC.view];
    
    _classicsAnaVC = [[MQClassicsAnaViewController alloc] initWithStyle:(UITableViewStylePlain)];
    _classicsAnaVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _classicsAnaVC.readVC = self;
    [_mainScrollView addSubview:_classicsAnaVC.view];
    
    _emtionVC = [[MQEmotionViewController
                  alloc] initWithStyle:(UITableViewStylePlain)];
    _emtionVC.view.frame = CGRectMake(SCREEN_WIDTH * 2, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _emtionVC.readMainVC = self;
    [_mainScrollView addSubview:_emtionVC.view];
    
    
    
}

// 切换视图
- (void)changeViews:(UISegmentedControl *)segment{
    switch (segment.selectedSegmentIndex) {
        case 0:
            _mainScrollView.contentOffset = CGPointMake(0, 0);
            break;
        case 1:
            _mainScrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
            break;
        case 2:
            _mainScrollView.contentOffset = CGPointMake(SCREEN_WIDTH * 2, 0);
            break;
            
        default:
            break;
    }

}
// 滑动视图改变segmentControl的选项
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int offSet = scrollView.contentOffset.x;
    if (offSet == 0) {
        _segment.selectedSegmentIndex = 0;
    }else if (offSet == SCREEN_WIDTH){
        _segment.selectedSegmentIndex = 1;
    }else if (offSet == SCREEN_WIDTH * 2){
        _segment.selectedSegmentIndex = 2;
    }
    
}
//右上角设置按钮
- (void)setting:(UIBarButtonItem *)barButton
{
    MQPopupView *view = [MQPopupView defaultPopupView];
    view.parentVC = self;
    [self lew_presentPopupView:view animation:[LewPopupViewAnimationDrop new] dismissed:^{
        NSLog(@"动画结束");
        
    }];
}


@end
